package com.pengheng.bigdata.spark.core.wc

import org.apache.spark.api.java.JavaRDD.fromRDD
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Spark01_WordCount {

  def main(args: Array[String]): Unit = {
    //TODO 建议和spark框架连接

    //TODO 执行业务操作
    val sparkConf = new SparkConf().setMaster("local").setAppName("WordCount")
    val sc = new SparkContext(sparkConf)

    val lines = sc.textFile("datas")
    val words = lines.flatMap(_.split(" "))
    val wordGroup = words.groupBy(word => word)

    val wordToCount = wordGroup.map {
      case (word, list) => {
        (word, list.size)
      }
    }
    val array = wordToCount.collect()
    array.foreach(println)
    //TODO 关闭连接
    sc.stop()
  }
}
